package com.qijian.maindata.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qijian.common.utils.mybatis.WhereEntityTool;
import com.qijian.maindata.domain.Item;
import com.qijian.maindata.domain.ProductAttribute;
import com.qijian.maindata.domain.ProductAttributeItem;
import com.qijian.maindata.domain.query.ItemQuery;
import com.qijian.maindata.domain.query.ProductAttributeQuery;
import com.qijian.maindata.domain.vo.ItemVo;
import com.qijian.maindata.mapper.ItemMapper;
import com.qijian.maindata.service.IItemService;
import com.qijian.maindata.service.IProductAttributeItemService;
import com.qijian.maindata.service.IProductAttributeService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 物料档案Service业务层处理
 *
 * @author qijian
 * @date 2021-09-01
 */
@Service
@RequiredArgsConstructor
public class ItemServiceImpl extends ServiceImpl<ItemMapper, Item> implements IItemService {

    private final ItemMapper itemMapper;

    private final IProductAttributeItemService productAttributeItemService;

    private final IProductAttributeService productAttributeService;

    @Override
    public Page<ItemVo> pageVo(Page<Item> page, Wrapper<ItemQuery> queryWrapper) {
        Page<ItemVo> itemVoPage = itemMapper.selectPageVo(page, queryWrapper);
        ProductAttributeQuery query = new ProductAttributeQuery();
        List<ProductAttribute> data = productAttributeService.list(WhereEntityTool.invoke(query));
        if (CollectionUtils.isNotEmpty(itemVoPage.getRecords())) {
            List<ProductAttributeItem> itemAttrs = productAttributeItemService.list(new LambdaQueryWrapper<ProductAttributeItem>().in(ProductAttributeItem::getItemId, itemVoPage.getRecords().stream().map(ItemVo::getItemId).collect(Collectors.toList())));
            if (CollectionUtil.isNotEmpty(itemAttrs)) {
                Map<Long, List<ProductAttributeItem>> map = itemAttrs.stream().collect(Collectors.groupingBy(ProductAttributeItem::getItemId));
                // 合并启用的属性
                for (ItemVo lineProcessItemVo : itemVoPage.getRecords()) {
                    List<ProductAttributeItem> productAttributeItems = map.get(lineProcessItemVo.getItemId());
                    if (lineProcessItemVo.getItemAttr() != null) {
                        if (CollectionUtil.isNotEmpty(productAttributeItems)) {
                            JSONArray jsonArray = JSONUtil.parseArray(lineProcessItemVo.getItemAttr());
                            productAttributeItems.stream().forEach(m->{
                                if (jsonArray !=null && jsonArray.stream().filter(e-> {
                                    JSONObject jsonObject = (JSONObject) e;
                                    return m.getAttrName().equals(jsonObject.getStr("name"));
                                }).collect(Collectors.toList()).size() == 0) {
                                    JSONObject o = new JSONObject();
                                    o.put("name", m.getAttrName());
                                    o.put("value","");
                                    jsonArray.put(o);
                                }
                            });
                            lineProcessItemVo.setItemAttr(jsonArray.toString());
                        }

                        if (lineProcessItemVo.getItemAttr() != null) {
                            //jsonArray 按照对象属性排序
                            JSONArray objects = JSONUtil.parseArray(lineProcessItemVo.getItemAttr());
                            JSONArray jsonArray = new JSONArray();

                            if (CollectionUtils.isNotEmpty(data)) {
                                data.stream().forEach(e->{
                                    objects.forEach(o->{
                                        JSONObject jsonObject = JSONUtil.parseObj(o);
                                        if (jsonObject.getStr("name") !=null && jsonObject.getStr("name").equals(e.getName())) {
                                            jsonArray.put(jsonObject);
                                        }
                                    });
                                });
                                lineProcessItemVo.setItemAttr(jsonArray.toString());
                            }
                        }

                    }else {
                        if (CollectionUtil.isNotEmpty(productAttributeItems)) {
                            JSONArray jsonArray = new JSONArray();
                            productAttributeItems.stream().forEach(m -> {
                                JSONObject o = new JSONObject();
                                o.put("name", m.getAttrName());
                                o.put("value", "");
                                jsonArray.put(o);
                            });
                            lineProcessItemVo.setItemAttr(jsonArray.toString());
                        }
                    }
                }
            }

        }
        return itemVoPage;
    }

    @Override
    public List<Long> idsList(Set<Long> ids) {
        if (CollectionUtils.isEmpty(ids))
            return Collections.emptyList();
        return itemMapper.selectIdsList(ids);
    }
}
